package com.my12306.common.controller;

import com.my12306.common.enums.CodeEnum;
import com.my12306.common.model.po.ScheduleJobPo;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.service.ScheduleJobService;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:苏牧夕
 * @Date:2020/5/28 1:29
 * @Version 1.0
 */
@RestController
@RequestMapping("/schedule/job")
public class JobController {
    @Autowired
    ScheduleJobService scheduleJobService;

    @PostMapping("/sys/joblist")
    public ResponseData getListPage(@RequestBody PageVo pageVo) {
        System.out.println(pageVo);
        PageUtils jobListPage = scheduleJobService.getJobListPage(pageVo);
        return ResponseData.ok().put("page", jobListPage);
    }

    @GetMapping("/info/{jobId}")
    public ResponseData getJobInfo(@PathVariable("jobId") Long jobId) {
        ScheduleJobPo scheduleJobPo = scheduleJobService.queryJobInfoByJobId(jobId);
        return ResponseData.ok().put("schedule", scheduleJobPo);
    }


    @PostMapping("/cron/add")
    public ResponseData addJob(@RequestBody ScheduleJobPo scheduleJobPo) {

        Boolean flag = scheduleJobService.addCronJob(scheduleJobPo);
        if (flag) {
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.ADD_FAIL.getCode(), "Cron表达式或者bean或者参数错误，请重新输入");
    }

    @PutMapping("/cron/update")
    public ResponseData updateJob(@RequestBody ScheduleJobPo scheduleJobPo) {
        Boolean flag = scheduleJobService.updateCronJob(scheduleJobPo);
        if (flag) {
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.UPDATE_FAIL.getCode(), "修改任务失败，可能是Cron表达式或者bean名字错误!");
    }

    @PutMapping("/pause")
    public ResponseData pauseJob(@RequestBody List<Long> ids) {
        Boolean flag = scheduleJobService.pauseJob(ids);
        if (flag) {
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.UPDATE_FAIL.getCode(), CodeEnum.UPDATE_FAIL.getMessage());
    }

    @PutMapping("/resume")
    public ResponseData resumeJob(@RequestBody List<Long> ids) {
        Boolean flag = scheduleJobService.resumeJob(ids);
        if (flag) {
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.UPDATE_FAIL.getCode(), CodeEnum.UPDATE_FAIL.getMessage());
    }

    @DeleteMapping("/delete")
    public ResponseData delete(@RequestBody List<Long> ids) {
        Boolean flag = scheduleJobService.deleteJob(ids);
        if (flag) {
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.UPDATE_FAIL.getCode(), CodeEnum.UPDATE_FAIL.getMessage());
    }

    @PutMapping("/resumeAll")
    public ResponseData resumeAllJob() {
        Boolean flag = scheduleJobService.resumeAllJob();
        if (flag) {
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.UPDATE_FAIL.getCode(), CodeEnum.UPDATE_FAIL.getMessage());
    }

    @PutMapping("/pauseAll")
    public ResponseData pauseAllJob() {
        Boolean flag = scheduleJobService.pauseAllJob();
        if (flag) {
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.UPDATE_FAIL.getCode(), CodeEnum.UPDATE_FAIL.getMessage());
    }

}
